package com.lzw.bluetoothpractice.Fragment;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzw.bluetoothpractice.Adapters.RecoveryAction2ListAdapter;
import com.lzw.bluetoothpractice.Adapters.RecoveryActionList3Adapter;
import com.lzw.bluetoothpractice.Adapters.VideoAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.MyApplication;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.PlanBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.ArrayList;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayerStandard;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class ThridFragment extends Fragment {
    public ThridFragment() {
    }
    private Map<String, String> keyMap;



    private ListView listView;
    private ArrayList<String> datas;
    private JCVideoPlayerStandard currPlayer;
    private VideoAdapter adapter;
    private AbsListView.OnScrollListener onScrollListener;

    Button angle;
    Button temperture;
    Button distance;

    RecyclerView rvTodayActio3List;
    RecoveryActionList3Adapter recoveryAction3ListAdapter;
    private int firstVisible;//当前第一个可见的item
    private int visibleCount;//当前可见的item个数
    private Handler mHander=new Handler();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.third_target,container,false);
        rvTodayActio3List=v.findViewById(R.id.rv_plan2List);


        initPlan();

        initDatas();
        //initListener();
        return v;
    }

    private void initPlan(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/doctor/list");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
        String userid = SharePreferenceUtils.getString(getContext(),"userId",null);
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getPlan(Long.parseLong("3"),"1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("计划2：",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                PlanBean planBean = JSON.parseObject(json.toString(), PlanBean.class);
                if(planBean.getCode()==200){

                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager2=new LinearLayoutManager(getContext());
                    linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
                    rvTodayActio3List.setLayoutManager(linearLayoutManager2);
                    //   rvTodayActionList.addItemDecoration(new DividerItemDecoration(mcontext,DividerItemDecoration.VERTICAL));
                    recoveryAction3ListAdapter=new RecoveryActionList3Adapter(getContext());
                    recoveryAction3ListAdapter.updateData(planBean.getData());
                    rvTodayActio3List.setAdapter(recoveryAction3ListAdapter);



                }else {
                    Toast toast=Toast.makeText(getContext(),"查询我的医生失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }
    private void initDatas() {


    }

    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
}
